package com.zjsru.plan2024.codeBase.fifty;

import java.util.HashMap;
import java.util.Map;

/**
 * 1275. 找出井字棋的获胜者
 *
 * @Author: cookLee
 * @Date: 2024-07-11
 */
public class Tictactoe {

    /**
     * 主
     * \
     * 输入：moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
     * 输出："A"
     * 解释："A" 获胜，他总是先走。
     * "X  "    "X  "    "X  "    "X  "    "X  "
     * "   " -> "   " -> " X " -> " X " -> " X "
     * "   "    "O  "    "O  "    "OO "    "OOX"
     * \
     * 输入：moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
     * 输出："B"
     * 解释："B" 获胜。
     * "X  "    "X  "    "XX "    "XXO"    "XXO"    "XXO"
     * "   " -> " O " -> " O " -> " O " -> "XO " -> "XO "
     * "   "    "   "    "   "    "   "    "   "    "O  "
     * \
     * 输入：moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
     * 输出："Draw"
     * 解释：由于没有办法再行动，游戏以平局结束。
     * "XXO"
     * "OOX"
     * "XOX"
     * \
     *
     * @param args args
     */
    public static void main(String[] args) {
        Tictactoe tictactoe = new Tictactoe();
        int[][] moves = new int[][]{{0, 0}, {1, 1}, {2, 0}, {1, 0}, {1, 2}, {2, 1}, {0, 1}, {0, 2}, {2, 2}};
        System.out.println(tictactoe.tictactoe(moves));
    }

    /**
     * A存储行值
     */
    Map<Integer, Integer> rowA = new HashMap<>();
    /**
     * A存储列值
     */
    Map<Integer, Integer> colA = new HashMap<>();
    /**
     * A存储对角值
     */
    Map<Integer, Integer> diogA = new HashMap<>();

    /**
     * B存储行值
     */
    Map<Integer, Integer> rowB = new HashMap<>();
    /**
     * B存储列值
     */
    Map<Integer, Integer> colB = new HashMap<>();
    /**
     * B存储对角值
     */
    Map<Integer, Integer> diogB = new HashMap<>();

    public String tictactoe(int[][] moves) {
        for (int i = 0; i < moves.length; i++) {
            //先手
            if (i % 2 == 0) {
                if (this.helper(moves[i], rowA, colA, diogA)) {
                    return "A";
                }
            }
            //后手
            if (i % 2 == 1) {
                if (this.helper(moves[i], rowB, colB, diogB)) {
                    return "B";
                }
            }
        }
        //如果游戏以平局结束，则返回 "Draw"；如果仍会有行动（游戏未结束），则返回 "Pending"
        return moves.length == 9 ? "Draw" : "Pending";
    }

    /**
     * 判断每回合棋子值 助手类
     *
     * @param move 移动
     * @return boolean
     */
    private boolean helper(int[] move, Map<Integer, Integer> row, Map<Integer, Integer> col, Map<Integer, Integer> diog) {
        //横排满足三个
        row.put(move[0], row.getOrDefault(move[0], 0) + 1);
        if (row.get(move[0]) == 3) {
            return true;
        }
        //竖排满足三个
        col.put(move[1], col.getOrDefault(move[1], 0) + 1);
        if (col.get(move[1]) == 3) {
            return true;
        }
        //“/”形对角，从左上到右下
        if (move[0] + move[1] == 2) {
            diog.put(2, diog.getOrDefault(2, 0) + 1);
            if (diog.get(2) == 3) {
                return true;
            }
        }
        //"\"对角,从左上到右下
        if (move[0] == move[1]) {
            diog.put(0, diog.getOrDefault(0, 0) + 1);
            return diog.get(0) == 3;
        }
        return false;
    }

}
